# @Time:   2021/2/18 14:47
# @Author: Zong Wei
from time import sleep

from faker import Faker
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class BizPartnerPage(LoginPage):
    logger = Logger(__name__).get_log()
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='业务通用']")
    sub_menu = (By.XPATH, "//ul[@class='site-menu']//span[.='业务伙伴']")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")
    save_button = (By.ID, "lr-save")
    clear_button = (By.ID, "lr-clear")

    partner_name = (By.ID, "PartnerNameCN")
    short_name = (By.ID, "ShortNameCN")
    partner_code = (By.ID, "PartnerCode")
    source = (By.ID, "LeadSource")
    source_value = (By.XPATH, "//div[@id='LeadSource-option']//li[2]")
    address = (By.ID, "Address")
    contact_person = (By.ID, "ContactPerson")
    cell_number = (By.ID, "Tel")
    save_next = (By.ID, "btnInfomation")
    set_invalid = (By.ID, "lr-delete")

    remark = (By.ID, "Remark")
    confirm_button = (By.XPATH, "//a[.='确认']")
    first_row = (By.XPATH, "//span[.='地址']//following::tr[1]")

    partner_name_value = Faker(locale='zh_CN').company()

    def __init__(self):
        self.driver = LoginPage().login()
        self.wait = WebDriverWait(self.driver, 10)

    def biz_partner_home(self):
        self.logger.info("登录系统成功")
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.sub_menu)).click()
        sleep(1)
        return self.driver

    def add_partner(self):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.element_to_be_clickable(self.add_button)).click()
        self.logger.info("添加业务伙伴")
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.partner_name)).send_keys(self.partner_name_value)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.short_name)).send_keys(self.partner_name_value)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.partner_code)).send_keys('TEST')
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.source)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.source_value)).click()
        self.wait.until(ec.presence_of_element_located(self.address)).send_keys(Faker().address())
        self.wait.until(ec.presence_of_element_located(self.contact_person)).send_keys('Mike')
        self.wait.until(ec.presence_of_element_located(self.cell_number)).send_keys('13412345678')
        self.wait.until(ec.element_to_be_clickable(self.save_next)).click()
        sleep(1)
        self.logger.info("新增完成")

    def search_partner(self):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.partner_name)).send_keys(self.partner_name_value)
        self.wait.until(ec.presence_of_element_located(self.partner_code)).send_keys('TEST')
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        self.wait.until(ec.element_to_be_clickable(self.clear_button)).click()
        self.logger.info("查询完成")

    def edit_partner(self):
        self.search_partner()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.partner_code)).send_keys('_Ed')
        self.wait.until(ec.presence_of_element_located(self.remark)).send_keys(Faker().address())
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.save_next)).click()
        self.logger.info("编辑修改完成")

    def del_partner(self):
        self.search_partner()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.del_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("已设为无效")


if __name__ == '__main__':
    pg = BizPartnerPage()
    pg.biz_partner_home()
    pg.edit_partner()

